package com.gl.order.security.model;

import cn.hutool.core.util.StrUtil;
import com.gl.order.entity.TUser;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @Author: liangSY
 * @Date: 2021/6/8 15:58
 * @ClassName: SecurityUserDetails
 */
@Data
public class SecurityUser implements UserDetails, Serializable {

    private TUser currentUserInfo;

    // 当前权限
    private List<String> permissionsList;

    public SecurityUser(){
    }

    public SecurityUser(TUser user){
        if(user != null){
            this.currentUserInfo = user;
        }
    }


    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        Collection<GrantedAuthority> authorities = new ArrayList<>();
        permissionsList.stream().forEach(permission -> {
            if(StrUtil.isNotBlank(permission)){
                SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority(permission);
                authorities.add(simpleGrantedAuthority);
            }
        });
        return authorities;
    }

    @Override
    public String getPassword() {
        return currentUserInfo.getPassword();
    }

    @Override
    public String getUsername() {
        return currentUserInfo.getAccount();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
